package com.lf.web.service.system.config;

import com.lf.data.ClusterUtil;
import com.lf.web.service.ResponseCode;
import com.lf.web.service.ResponseInfo;
import com.lf.web.service.ServiceSupport;
import org.apache.log4j.Logger;

import java.util.Date;

public class ConfigAddService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(ConfigAddService.class);
    private ConfigDAO configDAO;

    public void setConfigDAO(ConfigDAO configDAO) {
        this.configDAO = configDAO;
    }

    public String execute() {
        String type = form.getString("type");
        if (type.equals("save")) {
            return save();
        } else {
            return add();
        }
    }

    private String add() {
        context.put("check", ClusterUtil.getCheck(Config.class));
        return ADD;
    }

    private String save() {
        Config config = new Config();
        boolean result = form.getCluster(config);
        LOGGER.debug("添加配置：" + config);
        if (result) {
            String id = config.setId();
            config.put(Config.CREATED, new Date());
            configDAO.insert(config);
            ResponseInfo msg = new ResponseInfo(ResponseCode.FINISH);
            msg.put("id", id);
            out(msg);
        } else {
            error(404);
            LOGGER.error("非法添加配置：" + config);
        }
        return NONE;
    }
}
